package cn.iocoder.yudao.framework.mybatis.core.service;

import cn.iocoder.yudao.framework.mybatis.core.mapper.CommonMapperX;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 通用服务类，封装 CommonMapperX 的常用操作
 * 
 * @author yudao
 */
@Service
public class CommonService
{
    @Autowired
    private CommonMapperX commonMapper;

    /**
     * 根据ID查询单条记录
     */
    public Map<String, Object> getById(String table, String cols, Object id) {
        return commonMapper.queryById(table, cols, id);
    }

    /**
     * 查询全部记录
     */
    public List<Map<String, Object>> getAll(String table, String cols, String orderby) {
        return commonMapper.queryAll(table, cols, orderby);
    }

    /**
     * 根据条件查询
     */
    public List<Map<String, Object>> getByParams(String table, String cols, Map<String, Object> params, String orderby) {
        return commonMapper.queryByParams(table, cols, params, orderby);
    }

    /**
     * 执行自定义SQL查询
     */
    public List<Map<String, Object>> query(String sql, Map<String, Object> params) {
        return commonMapper.query(sql, params);
    }

    /**
     * 插入记录（自动生成ID）
     */
    public int insert(String table, Map<String, Object> data) {
        return commonMapper.insertNoId(table, data, null);
    }

    /**
     * 插入记录（指定ID）
     */
    public int insertWithId(String table, Map<String, Object> data) {
        return commonMapper.insertWithId(table, data, null);
    }

    /**
     * 批量插入记录
     */
    public int insertBatch(String table, List<Map<String, Object>> dataList) {
        return commonMapper.insertBatchNoId(table, dataList, null);
    }

    /**
     * 根据ID更新记录
     */
    public int updateById(String table, Map<String, Object> data, Object id) {
        return commonMapper.updateById(table, data, id, null);
    }

    /**
     * 根据条件更新记录
     */
    public int updateByParams(String table, Map<String, Object> data, Map<String, Object> conditions) {
        return commonMapper.updateByParams(table, data, conditions);
    }

    /**
     * 根据ID删除记录
     */
    public int deleteById(String table, Object id) {
        return commonMapper.deleteById(table, id);
    }

    /**
     * 根据条件删除记录
     */
    public int deleteByParams(String table, Map<String, Object> conditions) {
        return commonMapper.deleteByParams(table, conditions);
    }

    /**
     * 统计记录数
     */
    public Long count(String table, Map<String, Object> conditions) {
        StringBuilder sql = new StringBuilder("SELECT COUNT(*) as count FROM ").append(table);
        if (conditions != null && !conditions.isEmpty()) {
            sql.append(" WHERE ");
            conditions.entrySet().forEach(entry -> 
                sql.append(entry.getKey()).append(" = #{values.").append(entry.getKey()).append("} AND ")
            );
            sql.setLength(sql.length() - 5); // 移除最后的 " AND "
        }
        
        List<Map<String, Object>> result = commonMapper.query(sql.toString(), conditions);
        return result.isEmpty() ? 0L : Long.valueOf(result.get(0).get("count").toString());
    }

    /**
     * 检查记录是否存在
     */
    public boolean exists(String table, Map<String, Object> conditions) {
        return count(table, conditions) > 0;
    }
}